package engine

import "fmt"

type ConcurrentEngine struct {
	Scheduler   Scheduler
	WorkerCount int
}

type Scheduler interface {
	Submit(Request)
	ConfigureMasterWorkerChan(chan Request)
	// TODO
	WorkerReady(chan Request)
	Run()
}

func (e *ConcurrentEngine) Run(seeds ...Request) {

	// in := make(chan Request)
	out := make(chan ParseResult)
	// e.Scheduler.ConfigureMasterWorkerChan(in)
	e.Scheduler.Run()

	//开启多个协程
	for i := 0; i < e.WorkerCount; i++ {
		// createWorker(in, out)
		createWorker(out, e.Scheduler)
	}

	for _, r := range seeds {
		e.Scheduler.Submit(r)
	}

	for {
		result := <-out
		for _, item := range result.Items {
			fmt.Printf("Got item:%v", item)
		}

		for _, request := range result.Requests {
			e.Scheduler.Submit(request)
		}
	}

}

// func createWorker(in chan Request, out chan ParseResult) {
// 	go func() {
// 		for {
// 			//
// 			request := <-in
// 			result, err := worker(request) //抓取和解析
// 			if err != nil {
// 				continue
// 			}
// 			out <- result
// 		}
// 	}()
// }

func createWorker(out chan ParseResult, s Scheduler) {
	in := make(chan Request)
	go func() {
		for {
			s.WorkerReady(in)
			request := <-in
			result, err := worker(request) //抓取和解析
			if err != nil {
				continue
			}
			out <- result
		}
	}()
}
